package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.order.dao.read.RecheckBatchReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.RecheckBatchWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.RecheckBatchExcelDTO;
import com.mcxx.modules.miniSalvation.order.entity.RecheckBatchEntity;
import com.mcxx.modules.miniSalvation.order.param.RecheckBatchParam;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.BatchNoUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@TaTransactional
public class RecheckBatchWriteService extends BaseService {

    @Autowired
    private RecheckBatchWriteDao recheckBatchWriteDao;
    @Autowired
    private RecheckBatchReadDao recheckBatchReadDao;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;


    public void insertRecheckBatch(CheckEntity checkEntity, UserAccountVo accountVo) {
        RecheckBatchEntity batchEntity = new RecheckBatchEntity();
        BeanUtils.copyProperties(checkEntity, batchEntity);
        batchEntity.setId(Constant.getUUID());
        batchEntity.setCreateDate(new Date());
        batchEntity.setState(Constant.BatchRecheckStat.RECHECKING);
        batchEntity.setBatchNo(checkEntity.getCheckBatchNo());
        batchEntity.setCreateBy(accountVo.getUsername());

        int rows = recheckBatchWriteDao.insertIgnoreNull(batchEntity);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }

    public void batchInsert(List<CheckEntity> checkEntityList, UserAccountVo accountVo){
        for(CheckEntity checkEntity:checkEntityList){
            insertRecheckBatch(checkEntity,accountVo);
        }
    }

    public int updateIgnoreNull(RecheckBatchEntity batchEntity) {
        return recheckBatchWriteDao.updateIgnoreNull(batchEntity);
    }


    public int delete(RecheckBatchEntity recheckBatch) {
        return recheckBatchWriteDao.delete(recheckBatch);
    }


    public int deleteBatch(String[] ids, String areaCode, String[] status) {
        return recheckBatchWriteDao.deleteIds(ids, areaCode, status);
    }


    public int deleteBatch(String batchNo) {
        return recheckBatchWriteDao.deleteBatch(batchNo);
    }

    public int updateStateByBatchNo(List<String> list){
        return recheckBatchWriteDao.updateStateByBatchNo(list);
    }

    public int updateStateByCheckId(String checkId){
        return recheckBatchWriteDao.updateStateByCheckId(checkId);
    }

    public int updateStateByCheckIds(List<String> checkId){
        return recheckBatchWriteDao.updateStateByCheckIds(checkId);
    }

    public ExcelInfo exportExcelRecheckResultList(RecheckBatchParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);

        String nowMonth = DateUtil.sdf2.format(new Date());
        String nowYear = nowMonth.substring(0,4)+"-01-01";
        nowMonth = nowMonth.substring(0,nowMonth.length()-2)+"01";
        param.setNowMonth(nowMonth);
        param.setNowYear(nowYear);
        List<RecheckBatchExcelDTO> list  = recheckBatchReadDao.exportBatchRecheck(param);
        String taskTitle = "";
        if(Constant.BusinessCode.DIBAO.equals(param.getBusinessType())){
            taskTitle = "低保批量核对列表信息";
            excelInfo = exportExcelService.exportOrgOpLogs(RecheckBatchExcelDTO.class,list,"低保批量核对列表信息.xls",
                    "低保批量核对列表信息","低保批量核对列表信息",false,null);
        }else {
            taskTitle = "特困批量核对列表信息";
            excelInfo = exportExcelService.exportOrgOpLogs(RecheckBatchExcelDTO.class,list,"特困批量核对列表信息.xls",
                    "特困批量核对列表信息","特困批量核对列表信息",true,"familyObjectType");
        }

        return excelInfo;
    }
}
